// Copyright (c) 2023 Institute of Software, Chinese Academy of Sciences
// Ripple is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
//          http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.

package ripple.common.entity;

import io.netty.buffer.ByteBuf;
import ripple.common.helper.TypeHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;



/**
 * @author Zhen Tang
 */
public final class NodeMetadata {
    private int id;
    private String address;
    private int port;

    public int getGroupId() {
        return groupId;
    }

    public void setGroupId(int groupId) {
        this.groupId = groupId;
    }

    private int groupId;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    private String type;

    public NodeMetadata(int id, String address, int port) {
        this.setId(id);
        this.setAddress(address);
        this.setPort(port);
    }

    public NodeMetadata(int id, String address, int port, int groupId) {
        this.setId(id);
        this.setAddress(address);
        this.setPort(port);
        this.setGroupId(groupId);
    }

    public int getId() {
        return id;
    }

    private void setId(int id) {
        this.id = id;
    }

    public String getAddress() {
        return address;
    }

    private void setAddress(String address) {
        this.address = address;
    }

    public int getPort() {
        return port;
    }

    private void setPort(int port) {
        this.port = port;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        NodeMetadata that = (NodeMetadata) o;
        return id == that.id &&
                port == that.port &&
                Objects.equals(address, that.address);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, address, port);
    }

    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append(this.getId());
        builder.append(",");
        builder.append(this.getAddress());
        builder.append(",");
        builder.append(this.getPort());
        builder.append(",");
        builder.append(this.getGroupId());
        return builder.toString();
    }

    public static NodeMetadata fromString(String s) {
        String[] info = s.split(",");
        int id = Integer.parseInt(info[0]);
        String address = info[1];
        int port = Integer.parseInt(info[2]);
        int groupId = Integer.parseInt(info[3]);

        return new NodeMetadata(id, address, port, groupId);
    }

    public static String ListToString(List<NodeMetadata> nodeMetadataList) {
        StringBuilder builder = new StringBuilder();
        for (NodeMetadata nodeMetadata : nodeMetadataList) {
            if (builder.length() != 0) {
                builder.append(";");
            }
            builder.append(nodeMetadata.toString());
        }
        return builder.toString();
    }

    public static List<NodeMetadata> ListFromString(String s) {
        List<NodeMetadata> ret = new ArrayList<>();
        String[] nodeInfo = s.split(";");
        for(String info : nodeInfo) {
            ret.add(NodeMetadata.fromString(info));
        }
        return ret;
    }

}
